package com.sgb.kjwl.view.ui.activity.device;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDeviceList3Binding;
import com.sgb.kjwl.di.device.DaggerDeviceComponent;
import com.sgb.kjwl.di.device.DeviceModule;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.self.publish.DevicePublishActivity;
import com.sgb.kjwl.view.ui.fragment.device.DeviceListFragment3;
import com.sgb.kjwl.viewmodel.device.DeviceListModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 设备出租列表
 */
@Deprecated
public class DeviceListActivity3 extends AppActivity {

    @Inject
    DeviceListModel3 viewModel;
    private ActivityDeviceList3Binding binding;
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private String[] titles;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerDeviceComponent.builder().appComponent(appComponent).deviceModule(new DeviceModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, DeviceListActivity3.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_device_list3);
        binding.ivBack.setVisibility(View.VISIBLE);
        binding.ivBack.setOnClickListener(this);
        binding.tvRelease.setOnClickListener(this);

        binding.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    closeKeybord();
                    EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_DEVICE_FILTER).setMessageData(binding.editSearch.getText().toString()));
                }
                return false;
            }
        });
    }

    @Override
    protected void init() {
        viewModel.getFilterTypeModel();
    }

    public void showTypeView(List<DictEntity3> datas) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        mFragments = new ArrayList<>();
        int size = datas.size();
        titles = new String[size];
        for (int i = 0; i < size; i++) {
            mFragments.add(DeviceListFragment3.newInstance(datas.get(i).getId(), i));
            titles[i] = datas.get(i).getName();
        }
        pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(titles.length - 1);
        binding.tablayout.setViewPager(binding.viewpager, titles);
    }

    public void changeType(int position) {
        binding.viewpager.setCurrentItem(position);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_release://发布
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    EventBus.getDefault().postSticky("transport_to_login");
                } else {
//                    EventBus.getDefault().postSticky(DevicePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_DEVICE_PUBLISH));
                    DevicePublishActivity.start(this);
                }
                break;
        }

    }
}
